package org.masterinftel.scrumpmt.adapters;

import java.util.List;

import org.masterinftel.scrumpmt.Constants;
import org.masterinftel.scrumpmt.R;
import org.masterinftel.scrumpmt.adapters.ProductsListAdapter.ViewHolder;
import org.masterinftel.scrumpmt.json.model.ProductoJSON;
import org.masterinftel.scrumpmt.json.model.TareaJSON;
import org.masterinftel.scrumpmt.json.model.Task;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class BacklogAdapter extends ArrayAdapter<TareaJSON> {

	private final Activity context;
	private final List<TareaJSON> tasksList;

	public BacklogAdapter(Activity context, List<TareaJSON> tasksList) {
		super(context, R.layout.backlogelement, tasksList);
		this.context = context;
		this.tasksList = tasksList;
	}

	static class ViewHolder {
		protected TextView name;
		protected TextView member;
		protected TextView sprint;
		protected TextView status;

	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View item = null;
		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			item = inflater.inflate(R.layout.backlogelement, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.name = (TextView) item.findViewById(R.id.BLtaskname);
			viewHolder.member = (TextView) item.findViewById(R.id.BLtaskmember);
			viewHolder.sprint = (TextView) item.findViewById(R.id.BLtasksprint);
			viewHolder.status = (TextView) item.findViewById(R.id.BLtaskstatus);
			item.setTag(viewHolder);
		} else {
			item = convertView;

		}
		ViewHolder holder = (ViewHolder) item.getTag();

		holder.name.setText(tasksList.get(position).getDes());
		Log.i(Constants.LOGTAG, "Colab: "+tasksList.get(position).getColabName());
		if (tasksList.get(position).getColabName() != null) {
			holder.member.setText(tasksList.get(position).getColabName());
		} else {
			holder.member.setText("");
		}
		if (tasksList.get(position).getSprint() != null) {
			holder.sprint.setText(tasksList.get(position).getSprint());
		} else {
			holder.sprint.setText("Sin asignar sprint");
		}
		if (tasksList.get(position).getEstado() == 1)
			holder.status.setText("To Do");
		if (tasksList.get(position).getEstado() == 2)
			holder.status.setText("Doing");
		if (tasksList.get(position).getEstado() == 3)
			holder.status.setText("Done");

		return (item);
	}

}
